<template>
  <div class="app-container table-app-container">
    <div class="fixed-table">
      <!-- <TableFilter /> -->
      <ActionBar v-if="!forSelection" />
      <PageableTable
        :columns="insideColumns"
        :loading="loading"
        :data="insideTableData"
        :total="totalElements"
        :page="currentPage"
        :page-size="pageSize"
        :table-height="tableHeight"
        :show-advanced-search="false"
        class="fixed-table-table"
        @page-change="onPageChange"
        @sort-change="onSortChange"
        @selection-change="onSelectionChange"
        @close-filter-item="onCloseFilterItem"
      >
        <template v-for="column in insideColumns">
          <el-table-column v-if="column.display" :key="column.key" :label="column.title" :prop="column.key" :width="column.width" :sortable="column.sortable">
            <template slot-scope="scope">
              <column-render :render="column.render" :row="scope.row" :column="column" />
            </template>
          </el-table-column>
        </template>
        <el-table-column label="操作" align="center" width="160" class-name="small-padding fixed-width">
          <template slot-scope="{row}">
            <el-button type="text" title="查看" size="mini" icon="el-icon-view" @click="view(row.id)" />
            <el-button type="text" title="编辑" size="mini" icon="el-icon-edit" @click="edit(row.id)" />
            <el-button type="text" title="删除" size="mini" icon="el-icon-delete" @click="remove(row.id)" />
          </template>
        </el-table-column>
      </PageableTable>
    </div>
    <FormDialog title="编辑" :show.sync="dialogFormVisible" width="40%" @on-ok="ok" @on-cancel="cancel">
      <EditForm :id="editId" ref="editForm" />
    </FormDialog>
    <ViewDialog title="查看" :show.sync="dialogViewVisible" width="40%" @on-cancel="viewCancel">
      <ViewForm :id="editId" />
    </ViewDialog>
  </div>
</template>

<script>
// secondary package based on el-pagination
import ViewDialog from '@neu/biz-core/components/ViewDialog'
import FormDialog from '@neu/biz-core/components/FormDialog'
import ViewForm from './components/viewForm'
// 编辑实例组件独立编写，需要传递id属性，判断新建还是更新操作
import EditForm from './components/editForm'
import ActionBar from './components/actionBar'
import databind from '@neu/biz-core/mixins/databind'
import { formatDate, isArray } from '@neu/biz-core/utils/utils'
import PageableTable from '@neu/biz-core/components/PageableTable'
import {
  page,
  remove
} from '@neu/dcam-ui/api/contract'
export default {
  name: 'ContractPage',
  components: {
    FormDialog,
    EditForm,
    ActionBar,
    PageableTable,
    ViewDialog,
    ViewForm
  },
  // 加入混入，使表格混入更多的api操作
  mixins: [databind],
  props: {
    forSelection: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      r: page,
      d: remove,
      insideColumns: [
        { title: '合同编号', width: 130, key: 'code', sortable: 'custom', display: true,
          render: (h, params) => { // 查看功能
            const biaoqian = this.forSelection === true ? 'div' : 'el-link'
            return h('span', [
              h(biaoqian, {
                class: 'font-12',
                attrs: { title: params.row.code },
                props: { type: 'primary' },
                on: { click: () => { this.forSelection === true ? {} : this.view(params.row.id) } }},
              params.row.code)
            ])
          }
        },
        { title: '合同名称', width: 200, key: 'name', display: true },
        { title: '签订日期', width: 150, key: 'signingDate', sortable: 'custom', display: true, render: (h, params) => { return h('span', formatDate(params.row.signingDate, 'YYYY-MM-DD')) } },
        { title: '供应商', width: 210, key: 'manufacturerName', display: true },
        { title: '联系人', width: 120, key: 'manufacturerContact', display: true },
        { title: '联系电话', width: 130, key: 'manufacturerTelephone', display: true },
        { title: '合同描述', key: 'description', display: true }
      ],
      dialogFormVisible: false,
      dialogViewVisible: false,
      editId: -1
    }
  },
  methods: {
    onCloseFilterItem(item) {
      this.$refs.filterForm.onFilterItemClose(item)
    },
    // 表格编辑按钮点击事件处理
    edit(id) {
      this.editId = id
      this.dialogFormVisible = true
    },
    // 编辑资产对话框确认事件处理
    ok() {
      // 直接调用表单实例的保存方法
      this.$refs.editForm.save().then(response => {
        if (response === false) return
        this.$message({
          showClose: true,
          message: '操作成功！',
          type: 'success'
        })
        // 调用pageable-table实例的mixin方法refresh()，更新表格
        this.refresh()
        // 关闭对话框
        this.dialogFormVisible = false
        this.$refs.editForm.reset()
      })
    },
    cancel() {
      this.editId = -1
      this.$refs.editForm.reset()
      // 关闭对话框
      this.dialogFormVisible = false
    },
    viewCancel() {
      this.editId = -1
      // 关闭对话框
      this.dialogViewVisible = false
    },
    view(id) {
      // 打开对话框
      this.editId = id
      this.dialogViewVisible = true
    }
  }
}
</script>
